﻿using System;
using System.Web;
using CPETwit.Data.Entity;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using CPETwit.Web.Service.Implementation;
using CPETwit.Web.Service.Interface;

namespace CPETwit.Web.Pages
{
    public class GetImage : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //write your handler implementation here.
            byte[] _buf = ((User)context.Session["login"]).avatar;
          
            context.Response.ContentType = "image/jpeg";
            Stream strm = new MemoryStream(_buf); ;
            byte[] buffer = new byte[4096];
            int byteSeq = strm.Read(buffer, 0, 4096);

            while (byteSeq > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, byteSeq);
                byteSeq = strm.Read(buffer, 0, 4096);
            }
            context.Response.BinaryWrite(buffer);
        }

        #endregion
    }
}
